import 'package:flutter/material.dart';
import 'package:flutter_love/view/personal_view.dart';

import 'cart_view.dart';
import 'home_view.dart';
import 'message_view.dart';

class MenuView extends StatefulWidget {
  const MenuView({Key? key}) : super(key: key);

  @override
  _MenuViewState createState() => _MenuViewState();
}

class _MenuViewState extends State<MenuView> {
  late int currentIndex;
  final pages = [const HomeView(), const MessageView(), const CartView(), const PersonalView()];

  final List<BottomNavigationBarItem> items = [
    const BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
    const BottomNavigationBarItem(icon: Icon(Icons.message), label: '消息'),
    const BottomNavigationBarItem(icon: Icon(Icons.shopping_cart), label: '购物车'),
    const BottomNavigationBarItem(icon: Icon(Icons.person), label: '个人中心')
  ];

  @override
  void initState() {
    super.initState();
    currentIndex = 0;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: pages[currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        items: items,
        currentIndex: currentIndex,
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          if (index != currentIndex) {
            setState(() {
              currentIndex = index;
            });
          }
        },
      ),
    );
  }
}
